<?php

class ShoppingCart 
{
    public $items;
    
    public function __construct() { }
    
    public function add($name, $price, $quantity)
    {
        $this->items[] = array($name => array(
            'Price' => $price,
            'Quantity' => $quantity
        ));
    }
    
    public function delete($name, $quantity)
    {
        $currentQuantity = $this->items[$name]["Quantity"];
        
        if ($currentQuantity < $quantity)
        {
            $this->items[$name]["Quantity"] = 1;
            return;
        }
        
        $this->items[$name]["Quantity"] = $currentQuantity - $quantity;
    }
    
    public function subTotal() 
    {
        $totalPrice = 0;
        
        foreach ($this->items as $key => $value)
        {
        	foreach ($value as $items=>$item)
        	{
        		$totalPrice += $item["Price"];	
        	}	
        }
        
        return $totalPrice;
    }
    
    public function display()
    {	
    	if (!isset($this->items))
    	{
    		echo "Der Warenkorb ist leer";
    		return;
    	}
    	
    	
		$output = '<table border="1">';
        $output .= '<tr><th>Name</th><th>Preis</th><th>Anzahl</th></tr>';
                
        foreach ($this->items as $key=>$value)
		{
			foreach ($value as $items=>$item)
			{
			    $output .= "<tr><td>" . $items . "</td><td>" . $item['Price'] . "</td><td>" . $item['Quantity'] . "</td></tr>";
			}
		}
                
        $total = $this->subTotal();
        $output .= "<tr><td>Summe: </td><td colspan='2' align='right'>" . $total ."</td></tr>";
                
        $output .= '</table>';
                
        return $output;
    }
}